package ar.edu.unq.wannaplay.dtos;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import ar.edu.unq.wannaplay.calendareable.Licence;
import ar.edu.unq.wannaplay.calendareable.Project;
import ar.edu.unq.wannaplay.employee.Register;
import ar.edu.unq.wannaplay.utils.DTOHelper;

public class RegisterDTO extends DTO<Register> {

    private static final long serialVersionUID = 1L;

    private int id;

    private List<LicenceDTO> licences;

    private List<ProjectDTO> projects;

    public RegisterDTO() {
        super();
        licences = new ArrayList<LicenceDTO>();
        projects = new ArrayList<ProjectDTO>();
        // Empty constructor, entirely made for bindingConstruction on
        // presentationModel
    }

    public RegisterDTO(final Register register) {
        super();
        licences = this.generateLicencesDTOs(register.getLicences());
        projects = this.generateProjectsDTOs(register.getProjects());
        id = register.getId();
    }

    private List<LicenceDTO> generateLicencesDTOs(final List<Licence> realLicencies) {
        List<LicenceDTO> licencesDtos = new LinkedList<LicenceDTO>();
        for (Licence currentProject : realLicencies) {
            licencesDtos.add(this.createLicenceDTO(currentProject));
        }

        return licencesDtos;
    }

    private LicenceDTO createLicenceDTO(final Licence currentProject) {
        return new LicenceDTO(currentProject);
    }

    private List<ProjectDTO> generateProjectsDTOs(final List<Project> projectsReal) {
        List<ProjectDTO> projectsDTOs = new LinkedList<ProjectDTO>();
        for (Project currentProject : projectsReal) {
            projectsDTOs.add(this.createProjectDTO(currentProject));
        }

        return projectsDTOs;
    }

    private ProjectDTO createProjectDTO(final Project currentProject) {
        return new ProjectInRegisterDTO(currentProject);
    }

    public List<LicenceDTO> getLicences() {
        return licences;
    }

    public void setLicences(final List<LicenceDTO> licences) {
        this.licences = licences;
    }

    public List<ProjectDTO> getProjects() {
        return projects;
    }

    public void setProjects(final List<ProjectDTO> projects) {
        this.projects = projects;
    }

    public int getId() {
        return id;
    }

    @Override
    public Register fromDto() {
        Register register = new Register();
        register.setId(this.getId());
        DTOHelper<Licence, LicenceDTO> helperL = new DTOHelper<Licence, LicenceDTO>();
        register.setLicences(helperL.fromDtos(new LinkedList<DTO<Licence>>(this.getLicences())));
        DTOHelper<Project, ProjectDTO> helperP = new DTOHelper<Project, ProjectDTO>();
        register.setProjects(helperP.fromDtos(new LinkedList<DTO<Project>>(this.getProjects())));
        return register;
    }

    public void setId(final int id) {
        this.id = id;
    }
}
